package com.sike.netty.im.server.handler;

import com.sike.netty.im.common.command.ListGroupMembersRequestCommand;
import com.sike.netty.im.common.command.ListGroupMembersResponseCommand;
import com.sike.netty.im.common.command.Message;
import com.sike.netty.im.common.command.MessageBuilder;
import com.sike.netty.im.server.service.BeanFactory;
import com.sike.netty.im.server.service.GroupService;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.List;


/**
 * 获取群聊成员
 *
 * @author chenssy
 * @date 2023/09/21
 */
public class ListGroupMemebersRequestHandler extends SimpleChannelInboundHandler<ListGroupMembersRequestCommand> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ListGroupMembersRequestCommand msg) throws Exception {
        GroupService groupService = (GroupService) BeanFactory.getServiceBean(GroupService.class);
        List<String> membersList = groupService.listGroupMember(msg.getGroupId());

        ListGroupMembersResponseCommand responseCommand = new ListGroupMembersResponseCommand(membersList);
        Message message = MessageBuilder.buildMessage(responseCommand,null,null);
        ctx.channel().writeAndFlush(message);
    }
}
